package thread;

/**
 * 线程的状态
 */

public class TestDemo7 {
    public static void main(String[] args) {
        Object lock = new Object();
        Thread t = new Thread(() -> {

        });
        t.start();
        synchronized (lock) {

        }
        System.out.println(t.getState());

    }

    //TIMED_WAITING:阻塞
    //sleep或者join(1000)都是在阻塞状态
    public static void main4(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        Thread.sleep(1000);//不加的话不知道是哪个线程先执行完了
        System.out.println(t.getState());
    }

    //Runnable:准备
    public static void main3(String[] args) {
        Thread t = new Thread(() -> {
            //此时线程在无限循环中,随叫随到
            while (true){

            }
        });
        t.start();
        System.out.println(t.getState());
    }

    //TERMINATED:终止
    public static void main2(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {

        });
        t.start();
        Thread.sleep(1000);//因为不确定是Main线程还是Thread线程执行完,这里让线程等1s就必定是线程执行完
        System.out.println(t.getState());
    }

    //New:未运行
    public static void main1(String[] args) {
        Thread t = new Thread(() -> {

        });
        System.out.println(t.getState());
        t.start();
    }
}
